var parentData, parentActive;
layui.use(['form', 'layer','element','laypage'], function () {
	var form = layui.form,
		element = layui.element;
		laypage = layui.laypage;
		$ = layui.jquery;
	
	var imgPop;
	window.active = {
		reload:async function() {
			let res=await ajaxAsyncNoTip('banner',{},'GET')
			//初始化图片
			var imgList = [],
			imgSource = res
			for (var i = 0; i < imgSource.length; i++) {
				if (imgSource[i])
					imgList.push(`<li><div class="uploadImgDiv"><img layer-src="${imgSource[i].img}" src="${imgSource[i].img}"></div><div class="operate"><span style="padding:10px;line-height:40px">${imgSource[i].name}</span><i class="layui-icon img_del" id="${imgSource[i].id}">&#xe640;</i></div></li>`);
			}
			$('#Images').html(imgList.toString());
		},
		addImg: () => {
			imgPop = top.layer.open({
				title: '新增详情图片',
				type: 2,
				area: ["1200px", "650px"],
				fixed: false, //不固定
				offset: '50px',
				maxmin: false,
				content: 'page/banner/addImg.html',
				success: async function (layero, index) {
					let cw = layero.find('iframe')[0].contentWindow;
					cw.parentActive = active; //赋予父窗体事件
					setTimeout(function () {
						top.layer.tips('点击此处返回', '.layui-layer-setwin .layui-layer-close', {
							tips: 3
						});
					}, 500);
				}
			});
		},
		saveImg: (imgurl) => {
			top.layer.close(imgPop);
			$('#Images').html($('#Images').html() +
				`<li><div class="uploadImgDiv"><img layer-src="${imgurl}" src="${imgurl}"></div><div class="operate"><i class="layui-icon img_del">&#xe640;</i></div></li>`);
		},
		
	};
	
	//操作
	$('.layui-btn').on('click', function () {
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});


	async function getInfo(){
		let res=await ajaxAsyncNoTip('banner',{},'GET')
			//初始化图片
			var imgList = [],
			imgSource = res
			for (var i = 0; i < imgSource.length; i++) {
				if (imgSource[i])
					imgList.push(`<li><div class="uploadImgDiv"><img layer-src="${imgSource[i].img}" src="${imgSource[i].img}"></div><div class="operate"><span style="padding:10px;line-height:40px">${imgSource[i].name}</span><i class="layui-icon img_del" id="${imgSource[i].id}">&#xe640;</i></div></li>`);
			}
			$('#Images').html(imgList.toString());
	}
	getInfo()
	//图片预览弹出层
	$("body").on("click", ".uploadImgDiv img", function () {
		var imgSrc = $(this).attr('src');
		//页面层
		top.layer.open({
			type: 1,
			title: '',
			area: ['65%', '70%'], //宽高
			shadeClose: true, //开启遮罩关闭
			end: function (index, layero) {
				return false;
			},
			content: `<div style="height:100%;display: flex;display: -webkit-flex;flex-direction: column;justify-content: center;align-items: center;"><img style="max-width:95%;width:55%;height:auto;t" src="${imgSrc}"/></div>`
		});
	});
	//删除单张图片
	$("body").on("click", ".img_del",function (e) {
		let id = e.currentTarget.id
		
		var _this = $(this);
		top.layer.confirm('确定删除图片吗？', {
			icon: 3,
			title: '提示信息'
		}, function (index) {
			_this.parents("li").hide(1000);
			setTimeout(function () {
				_this.parents("li").remove();
			}, 950);
			top.layer.close(index);
		});
		ajaxAsyncNoTip('banner/del',{id},'GET')	
	});

});